package com.example.mybatisplusdemo.web.controller;

import com.example.mybatisplusdemo.common.utls.Res;
import com.example.mybatisplusdemo.model.dto.PaymentRequestDTO;
import com.example.mybatisplusdemo.model.dto.PaymentResponseDTO;
import com.example.mybatisplusdemo.service.IPaymentRecordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;


/**
 *
 *  前端控制器
 *
 *
 * @author lxp
 * @since 2025-07-16
 * @version v1.0
 */
@RestController
@RequestMapping("/api/payment")
public class PaymentRecordController {

    private final Logger logger = LoggerFactory.getLogger( PaymentRecordController.class );

    @Autowired
    private IPaymentRecordService paymentService;

    @PostMapping("/create")
    public Res<PaymentResponseDTO> createPayment(@RequestBody PaymentRequestDTO paymentRequest) {
        PaymentResponseDTO response = paymentService.createPayment(paymentRequest);
        if (response.isSuccess()) {
            return Res.success("支付创建成功", response);
        }
        return Res.error(response.getMessage());
    }

    @GetMapping("/query/{paymentNo}")
    public Res<PaymentResponseDTO> queryPayment(@PathVariable String paymentNo) {
        PaymentResponseDTO response = paymentService.queryPayment(paymentNo);
        return Res.success("查询成功", response);
    }

    @PostMapping("/callback/{paymentNo}")
    public Res<Boolean> paymentCallback(@PathVariable String paymentNo,
                                        @RequestBody String callbackContent) {
        boolean result = paymentService.processPaymentCallback(paymentNo, callbackContent);
        if (result) {
            return Res.success("回调处理成功", true);
        }
        return Res.error("回调处理失败");
    }

    @PostMapping("/refund/{paymentNo}")
    public Res<Boolean> refund(@PathVariable String paymentNo) {
        boolean result = paymentService.refund(paymentNo);
        if (result) {
            return Res.success("退款成功", true);
        }
        return Res.error("退款失败");
    }
}

